var app = getApp()
Page({
  data: {
    isShowturn:false,
    detailsType:1,
    availincome:'',
    page: 0,
    noMoretip: false,
    showLoading: true,
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.title) {
      app.setNavtitle(e.title);
      that.setData({
        title: e.title
      })
    } else {
      app.setNavtitle('小金库');
    }
    app.setVersion(that);
  },
  onShow: function () {
    var that = this;
    that.onPullDownRefresh();
  },
  requestMoney:function(){
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_member_info'
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var sumMoney = 0;
          var moneyinfo = res.data.data;
          sumMoney = parseFloat(moneyinfo.money) + parseFloat(moneyinfo.deduct_ktx);
          sumMoney = sumMoney.toFixed(2);
          that.setData({
            moneyinfo: moneyinfo,
            sumMoney: sumMoney
          })
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestTurnremain: function () {
    var that = this;
    var data = {
      map: 'applet_city_tx_2_balance',
      money: that.data.availincome
    }
    console.log(data.money);
    if(data.money==''){
      app.errorTip(that, "请输入提现金额", 2000);
      return;
    }
    if (data.money > that.data.moneyinfo.deduct_ktx) {
      app.errorTip(that, "超出最大金额限制", 2000);
      return;
    }
    wx.showLoading({
      title: '加载中',
      mask: true,
      time: 100000
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          app.errorTip(that, res.data.data.msg, 2000);
          that.hideTurnmodal();
          setTimeout(function(){
            that.requestMoney();
          },1000)
        } else {
          console.log(res.data)
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  requestDetails: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_city_inout_history';
    data.page = page;
    data.type = that.data.detailsType;
    data.status = that.data.couponStatus;
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.detailsList;
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            detailsList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.detailsList);
        } else {
          if (page <= 0) {
            that.setData({
              detailsList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true,
      detailsType: 1
    });
    that.requestMoney();
    that.requestDetails();
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      that.requestDetails();
    }
  },
  // 转到余额金额
  availincomeChange:function(e){
    this.setData({
      availincome:e.detail.value
    })
  },
  // 全部转入
  allTurn:function(){
    this.setData({
      availincome: this.data.moneyinfo.deduct_ktx
    })
  },
  toggleDetailstype:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.setData({
      detailsType:type,
      detailsList:null,
      page: 0,
      noMoretip: false,
      showLoading: true
    })
    that.requestDetails();
  },
  // 提现
  redbagWithdraw: function () {
    wx.navigateTo({
      url: '/subpages/incomeWithdraw/incomeWithdraw'
    })
  },
  // 提现规则
  withdrawRules: function () {
    wx.navigateTo({
      url: '/subpages/withdrawRules/withdrawRules'
    })
  },
  // 充值
  walletRecharge: function () {
    wx.navigateTo({
      url: '/subpages/walletRecharge/walletRecharge'
    })
  },
  // 禁止滑动页面
  catchtouchstart:function(){
    console.log("触摸屏幕阻止冒泡");
  },
  showTurnmodal:function(){
    this.setData({
      isShowturn:true
    })
  },
  hideTurnmodal: function () {
    this.setData({
      isShowturn: false
    })
  },
  //核销二维码显示
  codeModelShow: function () {
    var that = this;
    that.requestCode('showCode');
  },
  //核销二维码弹窗关闭
  closeCodeModel: function () {
    var that = this;
    that.setData({
      codeShow: false
    })
  },
  requestCode: function (type) {
    var that = this;
    var data = {
      map: 'applet_member_coin_code'
    };
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            payCodeinfo: res.data.data
          })
          if(type=='showCode'){
            that.setData({
              codeShow: true
            })
          }
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  // 显示充值类型弹窗
  showRechargetype: function () {
    var that = this;
    that.setData({
      isRechargetype: true
    })
  },
  // 隐藏充值类型弹窗
  hideRechargetype:function(){
    var that = this;
    that.setData({
      isRechargetype: false
    })
  },
  confirmRechagretype:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    that.hideRechargetype();
    wx.navigateTo({
      url: '/subpages/walletRecharge/walletRecharge?type='+type
    })
  }
})